<template>
  <div class="dashboard-container">
    <!-- 统计卡片区域 -->
    <el-row :gutter="20" class="stats-row">
      <el-col :span="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-content">
            <div class="stats-icon sales-icon">
              <el-icon><Money /></el-icon>
            </div>
            <div class="stats-info">
              <div class="stats-title">总销售额</div>
              <div class="stats-value">￥{{ formatNumber(126560) }}</div>
              <div class="stats-trend">
                <span class="trend-item">
                  <el-icon class="trend-up"><ArrowUp /></el-icon>
                  周同比 {{ '12.00%' }}
                </span>
                <span class="trend-item">
                  <el-icon class="trend-up"><ArrowUp /></el-icon>
                  日同比 {{ '11.00%' }}
                </span>
              </div>
            </div>
          </div>
          <div class="stats-footer">
            <el-divider />
            <span>日均销售额 ￥{{ formatNumber(234.56) }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-content">
            <div class="stats-icon order-icon">
              <el-icon><ShoppingCart /></el-icon>
            </div>
            <div class="stats-info">
              <div class="stats-title">订单量</div>
              <div class="stats-value">{{ formatNumber(8846) }}</div>
              <div class="stats-trend">
                <span class="trend-item">
                  <el-icon class="trend-up"><ArrowUp /></el-icon>
                  周同比 {{ '8.50%' }}
                </span>
                <span class="trend-item">
                  <el-icon class="trend-down"><ArrowDown /></el-icon>
                  日同比 {{ '2.30%' }}
                </span>
              </div>
            </div>
          </div>
          <div class="stats-footer">
            <el-divider />
            <span>日订单量 {{ formatNumber(1234) }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-content">
            <div class="stats-icon payment-icon">
              <el-icon><CreditCard /></el-icon>
            </div>
            <div class="stats-info">
              <div class="stats-title">支付笔数</div>
              <div class="stats-value">{{ formatNumber(6560) }}</div>
              <div class="stats-trend">
                <span class="trend-item">
                  <el-icon class="trend-up"><ArrowUp /></el-icon>
                  周同比 {{ '15.20%' }}
                </span>
                <span class="trend-item">
                  <el-icon class="trend-up"><ArrowUp /></el-icon>
                  日同比 {{ '9.80%' }}
                </span>
              </div>
            </div>
          </div>
          <div class="stats-footer">
            <el-divider />
            <span>转化率 {{ '60%' }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stats-card" shadow="hover">
          <div class="stats-content">
            <div class="stats-icon activity-icon">
              <el-icon><TrendCharts /></el-icon>
            </div>
            <div class="stats-info">
              <div class="stats-title">运营活动效果</div>
              <div class="stats-value">{{ '78%' }}</div>
              <div class="stats-trend">
                <span class="trend-item">
                  <el-icon class="trend-up"><ArrowUp /></el-icon>
                  周同比 {{ '12.00%' }}
                </span>
                <span class="trend-item">
                  <el-icon class="trend-up"><ArrowUp /></el-icon>
                  日同比 {{ '11.00%' }}
                </span>
              </div>
            </div>
          </div>
          <div class="stats-footer">
            <el-divider />
            <span>活动参与度 {{ '85%' }}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 图表区域 -->
    <el-row :gutter="20" class="chart-row">
      <el-col :span="16">
        <el-card class="chart-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <span>销售趋势分析</span>
              <el-radio-group v-model="chartType" size="small">
                <el-radio-button label="week">本周</el-radio-button>
                <el-radio-button label="month">本月</el-radio-button>
                <el-radio-button label="quarter">本季度</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div ref="salesChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card class="chart-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <span>销售分布</span>
            </div>
          </template>
          <div ref="pieChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 访问统计区域 -->
    <el-row :gutter="20" class="chart-row">
      <el-col :span="24">
        <el-card class="chart-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <span>最近一周访问统计</span>
              <el-button type="primary" size="small" @click="refreshData">
                <el-icon><Refresh /></el-icon>
                刷新数据
              </el-button>
            </div>
          </template>
          <div ref="visitChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, nextTick, watch, onUnmounted } from 'vue'
import * as echarts from 'echarts'
import { 
  Money, 
  ShoppingCart, 
  CreditCard, 
  TrendCharts, 
  ArrowUp, 
  ArrowDown,
  Refresh 
} from '@element-plus/icons-vue'

// 响应式数据
const chartType = ref('week')
const salesChartRef = ref<HTMLElement>()
const pieChartRef = ref<HTMLElement>()
const visitChartRef = ref<HTMLElement>()

// 图表实例
let salesChart: echarts.ECharts | null = null
let pieChart: echarts.ECharts | null = null
let visitChart: echarts.ECharts | null = null

// 格式化数字
const formatNumber = (num: number) => {
  return num.toLocaleString()
}

// 初始化销售趋势图表
const initSalesChart = () => {
  if (!salesChartRef.value) return
  
  salesChart = echarts.init(salesChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      }
    },
    legend: {
      data: ['销售额', '订单量']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: [
      {
        type: 'value',
        name: '销售额',
        position: 'left',
        axisLabel: {
          formatter: '￥{value}'
        }
      },
      {
        type: 'value',
        name: '订单量',
        position: 'right',
        axisLabel: {
          formatter: '{value}'
        }
      }
    ],
    series: [
      {
        name: '销售额',
        type: 'line',
        smooth: true,
        data: [12000, 15000, 18000, 14000, 22000, 25000, 20000],
        itemStyle: {
          color: '#409EFF'
        },
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              { offset: 0, color: 'rgba(64, 158, 255, 0.3)' },
              { offset: 1, color: 'rgba(64, 158, 255, 0.1)' }
            ]
          }
        }
      },
      {
        name: '订单量',
        type: 'bar',
        yAxisIndex: 1,
        data: [120, 150, 180, 140, 220, 250, 200],
        itemStyle: {
          color: '#67C23A'
        }
      }
    ]
  }
  salesChart.setOption(option)
}

// 初始化饼图
const initPieChart = () => {
  if (!pieChartRef.value) return
  
  pieChart = echarts.init(pieChartRef.value)
  const option = {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    series: [
      {
        name: '销售分布',
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: true,
            fontSize: '18',
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: [
          { value: 1048, name: '线上商城' },
          { value: 735, name: '实体门店' },
          { value: 580, name: '代理商' },
          { value: 484, name: '其他渠道' }
        ]
      }
    ]
  }
  pieChart.setOption(option)
}

// 初始化访问统计图表
const initVisitChart = () => {
  if (!visitChartRef.value) return
  
  visitChart = echarts.init(visitChartRef.value)
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      data: ['访问量', '用户数', '转化率']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: [
      {
        type: 'value',
        name: '访问量/用户数',
        position: 'left'
      },
      {
        type: 'value',
        name: '转化率',
        position: 'right',
        axisLabel: {
          formatter: '{value}%'
        }
      }
    ],
    series: [
      {
        name: '访问量',
        type: 'bar',
        data: [320, 332, 301, 334, 390, 330, 320],
        itemStyle: {
          color: '#409EFF'
        }
      },
      {
        name: '用户数',
        type: 'bar',
        data: [120, 132, 101, 134, 90, 230, 210],
        itemStyle: {
          color: '#67C23A'
        }
      },
      {
        name: '转化率',
        type: 'line',
        yAxisIndex: 1,
        data: [15, 18, 12, 20, 25, 30, 28],
        itemStyle: {
          color: '#E6A23C'
        }
      }
    ]
  }
  visitChart.setOption(option)
}

// 刷新数据
const refreshData = () => {
  // 这里可以调用API刷新数据
  console.log('刷新数据')
}

// 监听图表类型变化
watch(chartType, () => {
  // 根据选择的类型更新图表数据
  console.log('图表类型变化:', chartType.value)
})

// 监听窗口大小变化
const handleResize = () => {
  salesChart?.resize()
  pieChart?.resize()
  visitChart?.resize()
}

onMounted(async () => {
  await nextTick()
  initSalesChart()
  initPieChart()
  initVisitChart()
  
  window.addEventListener('resize', handleResize)
})

// 组件卸载时清理
onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  salesChart?.dispose()
  pieChart?.dispose()
  visitChart?.dispose()
})
</script>

<style scoped lang="less">
.dashboard-container {
  .stats-row {
    margin-bottom: 20px;
  }

  .chart-row {
    margin-bottom: 20px;
  }

  .stats-card {
    height: 160px;
    transition: all 0.3s ease;

    &:hover {
      transform: translateY(-2px);
    }

    .stats-content {
      display: flex;
      align-items: center;
      margin-bottom: 10px;

      .stats-icon {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 15px;
        font-size: 24px;
        color: white;

        &.sales-icon {
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }

        &.order-icon {
          background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        }

        &.payment-icon {
          background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
        }

        &.activity-icon {
          background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
        }
      }

      .stats-info {
        flex: 1;

        .stats-title {
          font-size: 14px;
          color: #909399;
          margin-bottom: 8px;
        }

        .stats-value {
          font-size: 24px;
          font-weight: bold;
          color: #303133;
          margin-bottom: 8px;
        }

        .stats-trend {
          display: flex;
          flex-direction: column;
          gap: 4px;

          .trend-item {
            font-size: 12px;
            display: flex;
            align-items: center;
            gap: 4px;

            .trend-up {
              color: #67C23A;
            }

            .trend-down {
              color: #F56C6C;
            }
          }
        }
      }
    }

    .stats-footer {
      font-size: 12px;
      color: #909399;
      text-align: center;
    }
  }

  .chart-card {
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-weight: bold;
      color: #303133;
    }

    .chart-container {
      height: 400px;
      width: 100%;
    }
  }

  // 响应式设计
  @media (max-width: 1200px) {
    .stats-card {
      height: 140px;

      .stats-content {
        .stats-icon {
          width: 50px;
          height: 50px;
          font-size: 20px;
        }

        .stats-info {
          .stats-value {
            font-size: 20px;
          }
        }
      }
    }

    .chart-container {
      height: 300px;
    }
  }

  @media (max-width: 768px) {
    .dashboard-container {
      padding: 10px;
    }

    .stats-card {
      height: 120px;

      .stats-content {
        .stats-icon {
          width: 40px;
          height: 40px;
          font-size: 16px;
        }

        .stats-info {
          .stats-value {
            font-size: 18px;
          }
        }
      }
    }

    .chart-container {
      height: 250px;
    }
  }
}
</style>
